<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\service\BaseService;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\facade\Request;
use think\Response;

/**
 * Class ModuleSpecial
 *
 * @package app\exam\service\admin
 */
class ModuleSpecial
{

    /**
     * @Inject()
     *
     * @var \app\exam\logic\admin\ModuleSpecial
     */
    protected $moduleSpecialLogic;

    /**
     * 专项配置
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/4
     *
     * @param int $moduleId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function config(int $moduleId)
    {
        $data = $this->moduleSpecialLogic->getTreeList($moduleId);

        $data = $this->moduleSpecialLogic->parseTreeTopicCount($data);
        // 定义字段
        $keyList = [
            'title'         => ListItem::make()->title('专项名称'),
            'topic_count'   => ListItem::make()->title('题数'),
            'price'         => ListItem::make('custom')->title('价格'),
            'status'        => ListItem::make('custom')->title('状态'),
            'credit_redeem' => ListItem::make('custom')->title('积分抵扣购买'),
            'update_time'   => ListItem::make()->title('更新时间'),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 新建专项
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/31
     *
     * @return Response
     */
    public function create(int $moduleId)
    {
        $data = Request::post();
        $this->moduleSpecialLogic->createSpecial($moduleId, $data);

        return BaseService::parseToData([], 1, '保存成功');
    }

    /**
     * 编辑专项
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/31
     *
     * @param int $moduleId
     * @param int $specialId
     * @return Response
     */
    public function edit(int $moduleId, int $specialId)
    {
        $data = Request::put();
        $this->moduleSpecialLogic->editSpecial($specialId, $moduleId, $data);

        return BaseService::parseToData([], 1, '保存成功');
    }

    /**
     * 编辑专项某个字段
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/31
     *
     * @param int    $specialId
     * @param string $field
     * @return Response
     */
    public function editField(int $specialId, string $field)
    {
        $value = Request::put($field);

        $this->moduleSpecialLogic->editSpecialField($specialId, $field, $value);

        return BaseService::parseToData([], 1, '编辑成功');
    }

    /**
     * 删除专项
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/31
     *
     * @param int $specialId
     * @return Response
     */
    public function delete(int $specialId)
    {
        $this->moduleSpecialLogic->deleteSpecial($specialId);

        return BaseService::parseToData([], 1, '删除成功');
    }
}